// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2011 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Tokens;
using DotNetNuke.Services.FileSystem;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Charity
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The CharityEdit class is used to manage content 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    partial class CharityEdit : CGOBase
    {

        #region "Private Members"

        private int charityID = 0;

        #endregion

        public int CharityID
        {
            get
            {
                if (charityID == 0)
                {
                    if ((Request.QueryString["CharityID"] != null))
                    {
                        charityID = Int32.Parse(Request.QueryString["CharityID"]);
                    }
                }
                return charityID;
            }
        }

        #region "Event Handlers"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// Page_Load runs when the control is loaded 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void Page_Load(object sender, System.EventArgs e)
        {
            try
            {                
                // If this is the first visit to the page, bind the role data to the datalist 
                if (Page.IsPostBack == false)
                {                   
                    cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
                    BindData();
                }
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }        

        private void BindData()
        {
            if (CharityID>0)
            {
                // get content 
                CharityController ctrlCharity = new CharityController();
                CharityInfo objCharity = ctrlCharity.GetCharity(PortalId, CharityID);

                if ((objCharity != null))
                {
                    BindCharity(objCharity);
                }
                else
                {
                    // security violation attempt to access item not related to this Module 
                    Response.Redirect(Globals.NavigateURL(), true);
                }
            }
            else
            {
                cmdDelete.Visible = false;
                ctlAudit.Visible = false;
            }            
        }

        private void BindCharity(CharityInfo objCharity)
        {
            txtCharityName.Text = objCharity.Name;
            txtContact.Text = objCharity.Contact;
            txtWebSite.Text = objCharity.WebSite;
            txtEmail.Text = objCharity.Email;
            txtTelephone.Text = objCharity.Telephone;
            txtFax.Text = objCharity.Fax;
            txtAddress.Text = objCharity.Address;                        
            txtDescription.Text = objCharity.Description;

            chkDisplayImage.Checked = objCharity.ImageDisplay;
            ctlURL.UrlType = objCharity.ImageType;
            ctlURL.Url = objCharity.ImageURL;
            txtHeight.Text = objCharity.ImageHeight.ToString();
            txtWidth.Text = objCharity.ImageWidth.ToString();

            ctlAudit.CreatedByUser = objCharity.CreateByUserName;
            ctlAudit.CreatedDate = objCharity.CreatedDate.ToString();
            ctlAudit.LastModifiedByUser = objCharity.LastUpdateUserName;
            ctlAudit.LastModifiedDate = objCharity.LastUpdateDate.ToString();
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// cmdCancel_Click runs when the cancel button is clicked 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// cmdUpdate_Click runs when the update button is clicked 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CharityController ctrlCharity = new CharityController();

                CharityInfo objCharity = new CharityInfo();

                objCharity.PortalID = PortalId;
                objCharity.CharityID = CharityID;
                objCharity.LastUpdatedID = UserId;

                objCharity.Name = txtCharityName.Text;
                objCharity.Contact = txtContact.Text;
                objCharity.WebSite = txtWebSite.Text;
                objCharity.Email = txtEmail.Text;
                objCharity.Telephone = txtTelephone.Text;
                objCharity.Fax = txtFax.Text;
                objCharity.Address = txtAddress.Text;                
                objCharity.Description = txtDescription.Text;

                if (chkDisplayImage.Checked)
                {
                    if (ctlURL.UrlType == "F" && ctlURL.Url.StartsWith("FileID="))
                    {
                        int fieldID = int.Parse(ctlURL.Url.Substring(7));
                        FileController ctrlFile = new FileController();
                        FileInfo objFile = ctrlFile.GetFileById(fieldID, PortalId);
                        if (string.IsNullOrEmpty(txtWidth.Text) || txtWidth.Text.Trim() == "0")
                        {
                            txtWidth.Text = objFile.Width.ToString();
                        }

                        if (string.IsNullOrEmpty(txtHeight.Text) || txtHeight.Text.Trim() == "0")
                        {
                            txtHeight.Text = objFile.Height.ToString();
                        }
                    }
                }

                objCharity.ImageDisplay = chkDisplayImage.Checked;
                objCharity.ImageType = ctlURL.UrlType;
                objCharity.ImageURL = ctlURL.Url;

                if (string.IsNullOrEmpty(txtWidth.Text))
                    objCharity.ImageWidth = 0;
                else
                    objCharity.ImageWidth = int.Parse(txtWidth.Text.Trim());

                if (string.IsNullOrEmpty(txtHeight.Text))
                    objCharity.ImageHeight = 0;
                else
                    objCharity.ImageHeight = int.Parse(txtHeight.Text.Trim());

                ctrlCharity.SaveCharity(objCharity);
                
                // Redirect back to the portal home page 
                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }        

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// cmdDelete_Click runs when the delete button is clicked 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            try
            {
                // Only attempt to delete the item if it exists already 
                if (CharityID>0)
                {

                    CharityController ctrlCharity = new CharityController();
                    ctrlCharity.DeleteCharity(PortalId, CharityID);

                }

                // Redirect back to the portal home page 
                Response.Redirect(Globals.NavigateURL(), true);
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }
}
